#!/bin/sh
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

echo_default () {
	echo "# TETHER_ENABLED: Whether or not to run the /usr/bin/bb-wl18xx-tether daemon; set to no to disable." > /etc/default/bb-wl18xx
	echo "TETHER_ENABLED=yes" >> /etc/default/bb-wl18xx
	echo "" >> /etc/default/bb-wl18xx
	echo "# USE_CONNMAN_TETHER: Whether or not to just use connman tether inteface; set to no to disable." >> /etc/default/bb-wl18xx
	echo "USE_CONNMAN_TETHER=no" >> /etc/default/bb-wl18xx
	echo "" >> /etc/default/bb-wl18xx
	echo "# USE_WL18XX_IP_PREFIX: default IP block of SoftAP0 interface" >> /etc/default/bb-wl18xx
	echo "USE_WL18XX_IP_PREFIX=\"192.168.8\"" >> /etc/default/bb-wl18xx
	echo "" >> /etc/default/bb-wl18xx
	echo "# USE_INTERNAL_WL18XX_MAC_ADDRESS: use internal mac address; set to no to disable." >> /etc/default/bb-wl18xx
	echo "USE_INTERNAL_WL18XX_MAC_ADDRESS=yes" >> /etc/default/bb-wl18xx
	echo "" >> /etc/default/bb-wl18xx
	echo "# USE_WL18XX_MAC_ADDRESS: use custom mac address, for when work wifi starts sending deauthentication packet spam." >> /etc/default/bb-wl18xx
	echo "#USE_WL18XX_MAC_ADDRESS=\"EC:11:27:B:14:97\"" >> /etc/default/bb-wl18xx
	echo "" >> /etc/default/bb-wl18xx

	echo "# USE_WL18XX_POWER_MANAGMENT: (sudo iwconfig wlan0 power [on/off]). on = boot default, off is more reliable for accessing idle systems over time" >> /etc/default/bb-wl18xx
	echo "USE_WL18XX_POWER_MANAGMENT=off" >> /etc/default/bb-wl18xx
	echo "" >> /etc/default/bb-wl18xx

	echo "# USE_PERSONAL_SSID: set custom ssid" >> /etc/default/bb-wl18xx
	echo "USE_PERSONAL_SSID=\"BeagleBone\"" >> /etc/default/bb-wl18xx
	echo "" >> /etc/default/bb-wl18xx
	echo "# USE_PERSONAL_PASSWORD: set ssid password" >> /etc/default/bb-wl18xx
	echo "USE_PERSONAL_PASSWORD=\"BeagleBone\"" >> /etc/default/bb-wl18xx
	echo "" >> /etc/default/bb-wl18xx
	echo "# USE_GENERATED_DNSMASQ: use generated version of /etc/dnsmasq.d/SoftAp0; set to no so user can modify /etc/dnsmasq.d/SoftAp0" >> /etc/default/bb-wl18xx
	echo "USE_GENERATED_DNSMASQ=yes" >> /etc/default/bb-wl18xx
	echo "" >> /etc/default/bb-wl18xx
	echo "# USE_GENERATED_HOSTAPD: use generated version of /etc/hostapd.conf; set to no so user can modify /etc/hostapd.conf" >> /etc/default/bb-wl18xx
	echo "USE_GENERATED_HOSTAPD=yes" >> /etc/default/bb-wl18xx
	echo "" >> /etc/default/bb-wl18xx
	echo "# USE_APPENDED_SSID: appends mac address after SSID (aka -WXYZ, BeagleBone-WXYZ)" >> /etc/default/bb-wl18xx
	echo "USE_APPENDED_SSID=yes" >> /etc/default/bb-wl18xx
	echo "" >> /etc/default/bb-wl18xx
}

if [ ! -f /etc/default/bb-wl18xx ] ; then
	echo_default
else
	unset test_new_option
	test_new_option=$(cat /etc/default/bb-wl18xx | grep TETHER_ENABLED || true)
	if [ "x${test_new_option}" = "x" ] ; then
		echo_default
	fi

	unset test_new_option
	test_new_option=$(cat /etc/default/bb-wl18xx | grep USE_GENERATED_DNSMASQ || true)
	if [ "x${test_new_option}" = "x" ] ; then
		echo "" >> /etc/default/bb-wl18xx
		echo "# USE_GENERATED_DNSMASQ: use generated version of /etc/dnsmasq.d/SoftAp0; set to no so user can modify /etc/dnsmasq.d/SoftAp0" >> /etc/default/bb-wl18xx
		echo "USE_GENERATED_DNSMASQ=yes" >> /etc/default/bb-wl18xx
		echo "" >> /etc/default/bb-wl18xx
		echo "# USE_GENERATED_HOSTAPD: use generated version of /etc/hostapd.conf; set to no so user can modify /etc/hostapd.conf" >> /etc/default/bb-wl18xx
		echo "USE_GENERATED_HOSTAPD=yes" >> /etc/default/bb-wl18xx
		echo "" >> /etc/default/bb-wl18xx
	fi

	unset test_new_option
	test_new_option=$(cat /etc/default/bb-wl18xx | grep USE_WL18XX_IP_PREFIX || true)
	if [ "x${test_new_option}" = "x" ] ; then
		echo "" >> /etc/default/bb-wl18xx
		echo "# USE_WL18XX_IP_PREFIX: default IP block of SoftAP0 interface" >> /etc/default/bb-wl18xx
		echo "USE_WL18XX_IP_PREFIX=\"192.168.8\"" >> /etc/default/bb-wl18xx
		echo "" >> /etc/default/bb-wl18xx
	fi

	unset test_new_option
	test_new_option=$(cat /etc/default/bb-wl18xx | grep USE_INTERNAL_WL18XX_MAC_ADDRESS || true)
	if [ "x${test_new_option}" = "x" ] ; then
		echo "" >> /etc/default/bb-wl18xx
		echo "# USE_INTERNAL_WL18XX_MAC_ADDRESS: use internal mac address; set to no to disable." >> /etc/default/bb-wl18xx
		echo "#USE_INTERNAL_WL18XX_MAC_ADDRESS=yes" >> /etc/default/bb-wl18xx
		echo "" >> /etc/default/bb-wl18xx
	fi

	unset test_new_option
	test_new_option=$(cat /etc/default/bb-wl18xx | grep USE_PERSONAL_SSID || true)
	if [ "x${test_new_option}" = "x" ] ; then
		echo "" >> /etc/default/bb-wl18xx
		echo "# USE_PERSONAL_SSID: set custom ssid" >> /etc/default/bb-wl18xx
		echo "USE_PERSONAL_SSID=\"BeagleBone\"" >> /etc/default/bb-wl18xx
		echo "" >> /etc/default/bb-wl18xx
	fi

	unset test_new_option
	test_new_option=$(cat /etc/default/bb-wl18xx | grep USE_PERSONAL_PASSWORD || true)
	if [ "x${test_new_option}" = "x" ] ; then
		echo "" >> /etc/default/bb-wl18xx
		echo "# USE_PERSONAL_PASSWORD: set ssid password" >> /etc/default/bb-wl18xx
		echo "USE_PERSONAL_PASSWORD=\"BeagleBone\"" >> /etc/default/bb-wl18xx
		echo "" >> /etc/default/bb-wl18xx
	fi

	unset test_new_option
	test_new_option=$(cat /etc/default/bb-wl18xx | grep USE_APPENDED_SSID || true)
	if [ "x${test_new_option}" = "x" ] ; then
		echo "# USE_APPENDED_SSID: appends mac address after SSID (aka -WXYZ, BeagleBone-WXYZ)" >> /etc/default/bb-wl18xx
		echo "USE_APPENDED_SSID=yes" >> /etc/default/bb-wl18xx
		echo "" >> /etc/default/bb-wl18xx
	fi

	unset test_new_option
	test_new_option=$(cat /etc/default/bb-wl18xx | grep USE_WL18XX_MAC_ADDRESS || true)
	if [ "x${test_new_option}" = "x" ] ; then
		echo "# USE_WL18XX_MAC_ADDRESS: use custom mac address, for when work wifi starts sending deauthentication packet spam.." >> /etc/default/bb-wl18xx
		echo "#USE_WL18XX_MAC_ADDRESS=\"EC:11:27:B:14:97\"" >> /etc/default/bb-wl18xx
		echo "" >> /etc/default/bb-wl18xx
	fi

	unset test_new_option
	test_new_option=$(cat /etc/default/bb-wl18xx | grep USE_CONNMAN_TETHER || true)
	if [ "x${test_new_option}" = "x" ] ; then
		echo "# USE_CONNMAN_TETHER: Whether or not to just use connman tether inteface; set to no to disable." >> /etc/default/bb-wl18xx
		echo "USE_CONNMAN_TETHER=no" >> /etc/default/bb-wl18xx
		echo "" >> /etc/default/bb-wl18xx
	fi

	#when i added USE_CONNMAN_TETHER i broke this...
	unset test_new_option
	test_new_option=$(cat /etc/default/bb-wl18xx | grep TETHER_ENABLED || true)
	if [ "x${test_new_option}" = "x" ] ; then
		echo "# TETHER_ENABLED: Whether or not to run the /usr/bin/bb-wl18xx-tether daemon; set to no to disable." >> /etc/default/bb-wl18xx
		echo "TETHER_ENABLED=yes" >> /etc/default/bb-wl18xx
		echo "" >> /etc/default/bb-wl18xx
	fi

	unset test_new_option
	test_new_option=$(cat /etc/default/bb-wl18xx | grep USE_WL18XX_POWER_MANAGMENT || true)
	if [ "x${test_new_option}" = "x" ] ; then
		echo "# USE_WL18XX_POWER_MANAGMENT: (sudo iwconfig wlan0 power [on/off]). on = boot default, off is more reliable for accessing idle systems over time" >> /etc/default/bb-wl18xx
		echo "USE_WL18XX_POWER_MANAGMENT=off" >> /etc/default/bb-wl18xx
		echo "" >> /etc/default/bb-wl18xx
	fi
fi

connman_main_dir="/etc/connman"
if [ -d /etc/connman/main.conf/ ] ; then
	rm -rf /etc/connman/main.conf/ || true
fi

if [ ! -f /etc/connman/main.conf ] ; then
	if [ ! -d /etc/connman/ ] ; then
		mkdir -p /etc/connman/ || true
	fi
	echo "[General]" > /etc/connman/main.conf
	echo "PreferredTechnologies=ethernet,wifi" >> /etc/connman/main.conf
	echo "SingleConnectedTechnology=false" >> /etc/connman/main.conf
	echo "AllowHostnameUpdates=false" >> /etc/connman/main.conf
	echo "PersistentTetheringMode=true" >> /etc/connman/main.conf
	echo "NetworkInterfaceBlacklist=SoftAp0,usb0,usb1" >> /etc/connman/main.conf
else
	cp -v /etc/connman/main.conf /etc/connman/main.conf.bak
	echo "[General]" > /etc/connman/main.conf
	echo "PreferredTechnologies=ethernet,wifi" >> /etc/connman/main.conf
	echo "SingleConnectedTechnology=false" >> /etc/connman/main.conf
	echo "AllowHostnameUpdates=false" >> /etc/connman/main.conf
	echo "PersistentTetheringMode=true" >> /etc/connman/main.conf
	echo "NetworkInterfaceBlacklist=SoftAp0,usb0,usb1" >> /etc/connman/main.conf
fi

if [ ! -f /var/lib/connman/settings ] ; then
	if [ ! -d /var/lib/connman/ ] ; then
		mkdir -p /var/lib/connman/ || true
	fi

	echo "[global]" > /var/lib/connman/settings
	echo "OfflineMode=false" >> /var/lib/connman/settings
	echo "" >> /var/lib/connman/settings
	echo "[Wired]" >> /var/lib/connman/settings
	echo "Enable=true" >> /var/lib/connman/settings
	echo "Tethering=false" >> /var/lib/connman/settings
	echo "" >> /var/lib/connman/settings
	echo "[WiFi]" >> /var/lib/connman/settings
	echo "Enable=true" >> /var/lib/connman/settings
	echo "Tethering=false" >> /var/lib/connman/settings
	echo "" >> /var/lib/connman/settings
	echo "[Gadget]" >> /var/lib/connman/settings
	echo "Enable=false" >> /var/lib/connman/settings
	echo "Tethering=false" >> /var/lib/connman/settings
	echo "" >> /var/lib/connman/settings
	echo "[P2P]" >> /var/lib/connman/settings
	echo "Enable=false" >> /var/lib/connman/settings
	echo "Tethering=false" >> /var/lib/connman/settings
	echo "" >> /var/lib/connman/settings
	echo "[Bluetooth]" >> /var/lib/connman/settings
	echo "Enable=true" >> /var/lib/connman/settings
	echo "Tethering=false" >> /var/lib/connman/settings
fi

case "$1" in
	configure)
		systemctl enable bb-wl18xx-bluetooth.service || true
		systemctl enable bb-wl18xx-wlan0.service || true
	;;

	abort-upgrade|abort-remove|abort-deconfigure)
	;;

	*)
		echo "postinst called with unknown argument \`$1'" >&2
		exit 1
	;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0

